package test.testList;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class RenameToFile {

    public static List<String> getFiles(String path,String operDate,String operName) throws Exception{
        List<String> files = new ArrayList<String>();
        File file = new File(path);
        File[] tempList = file.listFiles();

        if(tempList != null && tempList.length > 0) {
            for (int i = 0; i < tempList.length; i++) {
                File file1 = tempList[i];
                String filePath = file1.getPath();
                //System.out.println(filePath);
                //文件
                if (tempList[i].isFile()) {
                    //修改名称
                    renameFile(file1,operDate,operName);
                }
                //文件夹
                if (tempList[i].isDirectory()) {

                    //递归调用
                    getFiles(filePath,operDate,operName);
                }
            }
        }

        return files;
    }

    /**
     * 修改名称
     * @param file
     * @param operDate
     * @param operName
     */
    private static void renameFile(File file,String operDate,String operName){
        String fileName = file.getName();
        String filePath = file.getPath();

        //点需要转义
        String[] splitName = fileName.trim().split("\\.");
        String newFileName = operDate+operName+"("+splitName[0]+")"+"."+splitName[1];
        //System.out.println(newFileName);
        String newPath = filePath.substring(0,filePath.lastIndexOf("\\")) + "\\"+newFileName;
        //System.out.println(newPath);
        file.renameTo(new File(newPath));
    }
}
